import tkinter as tk
from tkinter import colorchooser, ttk

class DrawingTool:
    def __init__(self, root):
        self.root = root
        self.root.title("绘画工具")
        self.root.geometry("800x600")
        
        # 设置默认参数
        self.brush_color = "black"
        self.brush_size = 5
        self.eraser_size = 20
        self.is_drawing = False
        
        # 创建UI组件
        self.create_widgets()
        
        # 绑定鼠标事件
        self.canvas.bind("<Button-1>", self.start_drawing)
        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.stop_drawing)
    
    def create_widgets(self):
        # 创建控制框架
        control_frame = ttk.Frame(self.root, padding="5")
        control_frame.pack(fill=tk.X)
        
        # 颜色选择按钮
        ttk.Button(control_frame, text="选择颜色", command=self.choose_color).pack(side=tk.LEFT, padx=5)
        
        # 画笔大小选择
        ttk.Label(control_frame, text="画笔大小:").pack(side=tk.LEFT, padx=5)
        self.size_var = tk.IntVar(value=self.brush_size)
        ttk.Combobox(control_frame, textvariable=self.size_var, values=[1, 3, 5, 10, 20, 30], width=5).pack(side=tk.LEFT, padx=5)
        
        # 橡皮擦按钮
        ttk.Button(control_frame, text="橡皮擦", command=self.use_eraser).pack(side=tk.LEFT, padx=5)
        
        # 清除画布按钮
        ttk.Button(control_frame, text="清除画布", command=self.clear_canvas).pack(side=tk.LEFT, padx=5)
        
        # 创建画布
        self.canvas = tk.Canvas(self.root, bg="white")
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        # 状态标签
        self.status_var = tk.StringVar(value=f"颜色: {self.brush_color} | 大小: {self.brush_size}")
        ttk.Label(self.root, textvariable=self.status_var).pack(side=tk.BOTTOM, fill=tk.X)
    
    def choose_color(self):
        # 打开颜色选择对话框
        color = colorchooser.askcolor(title="选择颜色")[1]
        if color:
            self.brush_color = color
            self.update_status()
    
    def use_eraser(self):
        # 保存当前颜色以便退出橡皮擦模式时恢复
        self.last_color = self.brush_color
        self.brush_color = "white"
        self.brush_size = self.eraser_size
        self.size_var.set(self.eraser_size)
        self.update_status()
    
    def update_status(self):
        # 更新状态标签
        self.status_var.set(f"颜色: {self.brush_color} | 大小: {self.brush_size}")
    
    def start_drawing(self, event):
        self.is_drawing = True
        # 记录起始点
        self.last_x = event.x
        self.last_y = event.y
        
        # 如果是橡皮擦模式，点击时也画点
        if self.brush_color == "white":
            self.draw(event)
    
    def draw(self, event):
        if self.is_drawing:
            # 获取当前画笔大小
            self.brush_size = self.size_var.get()
            
            # 绘制线条
            self.canvas.create_line(
                self.last_x, self.last_y, event.x, event.y,
                width=self.brush_size, fill=self.brush_color,
                capstyle=tk.ROUND, smooth=tk.TRUE
            )
            
            # 更新最后位置
            self.last_x = event.x
            self.last_y = event.y
            self.update_status()
    
    def stop_drawing(self, event):
        self.is_drawing = False
        # 如果退出橡皮擦模式，恢复原来的颜色
        if self.brush_color == "white" and hasattr(self, 'last_color'):
            self.brush_color = self.last_color
            self.update_status()
    
    def clear_canvas(self):
        # 清除画布上的所有内容
        self.canvas.delete("all")

if __name__ == "__main__":
    root = tk.Tk()
    app = DrawingTool(root)
    root.mainloop()

